package service

import (
	"fly/global"
	"fly/models"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"net/url"
)

const (
	BizConfigCacheKey = "BizConfigCacheKey:%d"
)

type BizConfigService struct {
	BaseService
}

// GetConfigById retrieves BizConfig by Id. Returns error if
// Id doesn't exist
func (c *BizConfigService) GetConfigById(id int64) (*models.BizConfig, error) {
	key := fmt.Sprintf(BizConfigCacheKey, id)
	return global.GetCache[*models.BizConfig](key, func() (*models.BizConfig, error) {
		o := orm.NewOrm()
		v := models.BizConfig{}
		err := o.QueryTable(new(models.BizConfig)).Filter("id", id).RelatedSel().One(&v)
		return &v, err
	})
}

// GetPaginateData 分页获取adminrole
func (c *BizConfigService) GetPaginateData(params url.Values) ([]*models.BizConfig, Pagination) {
	//搜索、查询字段赋值
	c.WhereField = append(c.WhereField, new(models.BizConfig).WhereField()...)

	var configs []*models.BizConfig
	o := orm.NewOrm().QueryTable(new(models.BizConfig))
	_, err := c.PaginateAndScopeWhere(o, params).All(&configs)
	if err != nil {
		return nil, c.Pagination
	}
	return configs, c.Pagination
}
